package service.temp.job.matching.poc.listingapi;

import feign.Headers;
import feign.RequestLine;
import lombok.extern.slf4j.Slf4j;
import service.temp.job.matching.poc.listingapi.dto.ListResult;
import service.temp.job.matching.poc.listingapi.dto.SearchCondition;
//import service.temp.job.matching.poc.listingapi.dto.SearchItem;
import service.temp.job.matching.poc.listingapi.dto.SearchItemDetail;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ListingSearchApi
 *
 * @author Chao Xiong (chxiong@coupang.com)
 */

public interface SearchApi {
    /**
     * https://api-forge.coupang.net/details?specId=5a30f07926918c0030ca6d62
     */
    @RequestLine("POST /v2/providers/listing_api/apis/api/v1/search")
    @Headers("Content-Type: application/json")
    ListResult search(@Nonnull SearchCondition condition);

//    default List<SearchItem> searchItemByQuery(@Nonnull String query) {
//        return search(new SearchCondition(query)).getProducts();
//    }

    default List<SearchItemDetail> searchItemByPaging(@Nonnull String query) {
        List<SearchItemDetail> mergeSearchResultList = new ArrayList<>();
        SearchCondition searchCondition = new SearchCondition(query);
        Set<Long> itemIdSet = new HashSet<>();
        int lastStepSize = 0, searchNoneCnt = 0;

        while (itemIdSet.size() <= 1000 && searchNoneCnt < 3) {
            searchCondition.setStart(lastStepSize);
            searchCondition.setLimit(100);

            ListResult searchResultDto = null;
            try {
                searchResultDto = search(searchCondition);
            } catch (Exception e) {
                System.err.println(query + " -- call search API failed! exception : " + e.toString());
            }

            if (searchResultDto.getProducts().size() <= 0) searchNoneCnt++;
            for (Map<String, Object> productVO : searchResultDto.getProducts()) {
                SearchItemDetail searchItemDetail = convert(productVO);
                if (null != searchItemDetail && !itemIdSet.contains(searchItemDetail.getItemId())) {
                    mergeSearchResultList.add(searchItemDetail);
                    itemIdSet.add(searchItemDetail.getItemId());
                    if (itemIdSet.size() >= 1000) break;
                }
            }

            lastStepSize += searchResultDto.getProducts().size();
        }

        return mergeSearchResultList;
    }

    default SearchItemDetail convert(Map<String, Object> itemMap) {
        try {
            SearchItemDetail searchItemDetail = new SearchItemDetail();
            searchItemDetail.setItemId(itemMap.get("itemId") instanceof Long ? (long) (itemMap.get("itemId")) : (Integer) (itemMap.get("itemId")));
            searchItemDetail.setProductId(itemMap.get("id") instanceof Long ? (long) (itemMap.get("id")) : (Integer) (itemMap.get("id")));
            searchItemDetail.setItemName((String) (itemMap.get("title")));
            searchItemDetail.setProductName((String) (itemMap.get("productTitle")));
            searchItemDetail.setAttribute((ArrayList) (itemMap.get("itemAttributes")));
            searchItemDetail.setBrand(null == itemMap.get("brands") ? "" : (String) (((ArrayList) (itemMap.get("brands"))).get(0)));
            searchItemDetail.setMainImage((String) (itemMap.get("imagePath")));
            return searchItemDetail;
        } catch (Exception e) {
            System.err.println("convert SearchItemDetail got error! cause : " + e.toString());
        }

        return null;
    }

}
